//
//  TTPassCertificateNumViewController.m
//  TaoTuo
//
//  Created by LG on 2020/8/10.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTPassCertificateNumViewController.h"
#import "TTPassDiamondInfoViewCell.h"
#import "TTSendButtonView.h"
#import "TTCertQueryResultModel.h"
#import "TTEstimateDetailViewController.h"
#import "TTEstimateDetailViewController.h"
#import "TTAutoEstimateDiamondListView.h"
#import "TTRecyclingSendViewController.h"

@interface TTPassCertificateNumViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *scorllView;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UITextField *certificateTF;

@property (nonatomic, strong) TTAutoEstimateDiamondListView *autoDiamondView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) TTCertQueryResultModel *model;

@property (nonatomic, strong) TTSendButtonView *btnView;

/// 证书验证结果
@property (nonatomic, strong) NSDictionary *zsResultDic;

@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) YYTimer *timer;
@property (nonatomic, assign) NSInteger timeCount;

@end

@implementation TTPassCertificateNumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorHex(#F7F7F7);
    
    [self setupSubviews];
    
    [self setupDatas];
    
    
}

- (void)timerAction
{
    self.timeCount++;
    self.hud.label.text = [NSString stringWithFormat:@"已等待%ld秒", self.timeCount];
}

/// 验证证书号
- (void)validationBtnClick
{
    if (self.certificateTF.text.length == 0) {
        [self.view makeToast:@"请输入证书号"];
        return;
    }
    
    self.timeCount = 0;
    [self createHUD];
    self.timer = [YYTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) repeats:YES];
    
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:certQuery_api parameters:@{@"param.certNo" : self.certificateTF.text, @"param.entrance" : @2} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        
        [self.dataArray removeAllObjects];
        self.zsResultDic = responseObject;
        TTCertQueryResultModel *model = [TTCertQueryResultModel modelWithJSON:responseObject];
        [self.dataArray addObject:@{@"leftTitle" : @"证书编号", @"rightTitle" : model.ReportNo}];
        [self.dataArray addObject:@{@"leftTitle" : @"形状", @"rightTitle" : model.Shape}];
        [self.dataArray addObject:@{@"leftTitle" : @"克拉重量", @"rightTitle" : model.DiaSize}];
        [self.dataArray addObject:@{@"leftTitle" : @"颜色等级", @"rightTitle" : model.Color}];
        [self.dataArray addObject:@{@"leftTitle" : @"净度等级", @"rightTitle" : model.Clarity}];
        [self.dataArray addObject:@{@"leftTitle" : @"切工等级", @"rightTitle" : model.Cut}];
        [self.dataArray addObject:@{@"leftTitle" : @"抛光", @"rightTitle" : model.Polish}];
        [self.dataArray addObject:@{@"leftTitle" : @"对称", @"rightTitle" : model.Sym}];
        [self.dataArray addObject:@{@"leftTitle" : @"荧光", @"rightTitle" : model.Flour}];
        
        self.model = model;
        
        self.autoDiamondView.lookZSBtn.hidden = model.ReportNo.length <= 1;
        [self.autoDiamondView setDataArray:self.dataArray certUrlString:model.CertFile];
        
        [self setScrollViewHeight];
        
        [self.btnView setButtonEnabled:YES];
        
        [self.hud hideAnimated:YES];
        [self.timer invalidate];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        [MBProgressHUD dismissAnimationOnView:[UIApplication sharedApplication].keyWindow];
        [self.hud hideAnimated:YES];
        [self.timer invalidate];
    } showLoadingInView:self.view];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self validationBtnClick];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (string.length == 0) {
        return YES;
    }
    return textField.text.length < 12;
}

- (void)createHUD
{
    self.hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    self.hud.label.text = @"已等待0秒";
    self.hud.contentColor = [UIColor whiteColor];
    self.hud.margin = 10;
    self.hud.removeFromSuperViewOnHide = YES;
    self.hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    // 设置弹框背景颜色
    self.hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
    self.hud.animationType = MBProgressHUDAnimationZoomOut;
    
}

/// 预览证书
- (void)lookZsBtnClick
{
    [TTLoginSingle lookCertificateWithUrlString:self.model.CertFile];
}

- (void)setupSubviews
{
    _scorllView = [[UIScrollView alloc] init];
//    _scorllView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scorllView];
    [_scorllView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).mas_offset(-70);
    }];
    
    _topView = [[UIView alloc] init];
    _topView.backgroundColor = [UIColor whiteColor];
    [_scorllView addSubview:_topView];
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scorllView);
        make.height.mas_equalTo(130);
        make.left.right.equalTo(self.view);
    }];
    
    UILabel *zsLabel = [UILabel labelWithTitle:@"GIA证书" font:[UIFont boldSystemFontOfSize:18]];
    [_topView addSubview:zsLabel];
    [zsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.top.mas_equalTo(25);
    }];
    
    _certificateTF = [[UITextField alloc] init];
    _certificateTF.placeholder = @"请输入证书号";
    _certificateTF.font = [UIFont systemFontOfSize:15];
    _certificateTF.delegate = self;
    _certificateTF.keyboardType = UIKeyboardTypeNumberPad;
    _certificateTF.returnKeyType = UIReturnKeySearch;
    _certificateTF.enablesReturnKeyAutomatically = YES;
    [_topView addSubview:_certificateTF];
    [_certificateTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zsLabel.mas_bottom).mas_offset(10);
        make.left.equalTo(zsLabel);
        make.right.equalTo(self.view).mas_offset(-150);
        make.height.mas_equalTo(35);
    }];
    
    UIButton *validationBtn = [UIButton createButtonWithTitle:@"验证证书号" titleColor:UIColorHex(#8B57E6) titleFontSize:13 borderColor:UIColorHex(#8B57E6) cornerRadius:4 contentEdgeInsets:UIEdgeInsetsZero target:self action:@selector(validationBtnClick)];
    [_topView addSubview:validationBtn];
    [validationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).mas_offset(-50);
        make.centerY.equalTo(self.certificateTF);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(25);
    }];
    
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = UIColorHex(#999999);
    [_topView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.certificateTF.mas_bottom).mas_offset(1);
        make.left.equalTo(self.certificateTF);
        make.right.equalTo(self.view).mas_offset(-50);
        make.height.mas_equalTo(1);
    }];
    
    UILabel *hintLabel = [UILabel labelWithTitle:@"GIA为全球钻石数据，查询大约需要5~10秒钟！" color:UIColorHex(#DE4D4D) fontSize:11];
    [_topView addSubview:hintLabel];
    [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView).mas_offset(8);
        make.left.equalTo(lineView);
    }];
    
    self.autoDiamondView = [[TTAutoEstimateDiamondListView alloc] init];
    self.autoDiamondView.lookZSBtn.hidden = YES;
    [_scorllView addSubview:self.autoDiamondView];
    [_autoDiamondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).mas_offset(18);
        make.left.mas_equalTo(15);
        make.right.equalTo(self.view).mas_offset(-15);
        make.height.mas_equalTo(200);
    }];
    
    
    [self setScrollViewHeight];
    
    @weakify(self)
    TTSendButtonView *btnView = [TTSendButtonView createSendButtonViewAndClickSendButtonBlock:^{
        @strongify(self)
        [self sendBtnClick];
    } startValuationBtnBlock:^{
        @strongify(self)
        [self startValuationBtnClick];
    }];
    [self.view addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scorllView.mas_bottom).mas_offset(10);
        make.height.equalTo(@40);
        make.left.equalTo(@15);
        make.right.equalTo(@-15);
    }];
    self.btnView = btnView;
    
}

- (void)setScrollViewHeight
{
    [self.view layoutIfNeeded];
    
    self.scorllView.contentSize = CGSizeMake(kScreenWidth, self.autoDiamondView.bottom + 30);
}

- (void)setupDatas
{
    [self.dataArray removeAllObjects];
    
    [self.dataArray addObject:@{@"leftTitle" : @"证书编号", @"rightTitle" : @"——"}];
    [self.dataArray addObject:@{@"leftTitle" : @"形状", @"rightTitle" : @"——"}];
    [self.dataArray addObject:@{@"leftTitle" : @"克拉重量", @"rightTitle" : @"——"}];
    [self.dataArray addObject:@{@"leftTitle" : @"颜色等级", @"rightTitle" : @"——"}];
    [self.dataArray addObject:@{@"leftTitle" : @"净度等级", @"rightTitle" : @"——"}];
    [self.dataArray addObject:@{@"leftTitle" : @"切工等级", @"rightTitle" : @"——"}];
    [self.dataArray addObject:@{@"leftTitle" : @"抛光", @"rightTitle" : @"——"}];
    [self.dataArray addObject:@{@"leftTitle" : @"对称", @"rightTitle" : @"——"}];
    [self.dataArray addObject:@{@"leftTitle" : @"荧光", @"rightTitle" : @"——"}];
    
    
    [self.autoDiamondView setDataArray:self.dataArray certUrlString:@""];
    
    [self setScrollViewHeight];
}

- (void)sendBtnClick
{
    [self commitZSInfoSuccess:^(NSString *infoID) {
        TTRecyclingSendViewController *sendVC = [[TTRecyclingSendViewController alloc] init];
        sendVC.Reclaim_InformationId = infoID;
        [[LLRouteTool getCurrentNavigationController] pushViewController:sendVC animated:YES];
    }];
    
}

- (void)startValuationBtnClick
{
    [self commitZSInfoSuccess:^(NSString *infoID) {
        
        TTEstimateDetailViewController *detailVC = [[TTEstimateDetailViewController alloc] init];
        detailVC.Reclaim_InformationId = infoID;
        [[LLRouteTool getCurrentNavigationController] pushViewController:detailVC animated:YES];
        
    }];
    
}

- (void)commitZSInfoSuccess:(void(^)(NSString *infoID))success
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.zsResultDic];
    dic[@"RealName"] = [TTLoginSingle sharedSingle].loginModel.RealName;
    dic[@"SalesmanAccountId"] = [TTLoginSingle sharedSingle].loginModel.SalesmanAccountId;
    dic[@"SalesmanAccountName"] = [TTLoginSingle sharedSingle].loginModel.SalesmanAccountName;
    dic[@"SalesmanMobile"] = [TTLoginSingle sharedSingle].loginModel.SalesmanMobile;
    dic[@"RealName"] = [TTLoginSingle sharedSingle].loginModel.RealName;
    
    [[LLNetworkManagerTool sharedManager] postJsonWithUrlString:autoPrice_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject[@"Reclaim_InformationId"]);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        
    } showLoadingInView:[UIApplication sharedApplication].keyWindow];
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (UIView *)listView
{
    return self.view;
}

@end
